<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mobile_Trips_module extends CI_Module
{
    public $data ;
    public $sid ;
    public $trips_model;

    public function __construct(){
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->data = $GLOBALS['config'];
        $this->load->model('admin.area_model');
        $this->load->model('trips.trips_model');
        
    }

    public function index()
    {
        $keyword = $where = '';
        $sid  = $this->sid ;
        if(!empty($_REQUEST['keyword']))
        {
            $keyword = addslashes($_REQUEST['keyword']);
            $where .= " AND (subject LIKE '%$keyword%' OR message LIKE '%$keyword%') ";
        }
        if(!empty($_GET['category']))
        {
            $category = intval($_GET['category']);
            $where .= " AND category=$category ";
        }

        $pagesize = 8;
        $page = !empty($_GET['page']) && $_GET['page']>0 ? intval($_GET['page']) : 1;

        $this->data['datalist'] = $this->trips_model->trips_list($pagesize, $page,"status=0 and sid = $sid".$where);
        foreach ($this->data['datalist'] as $k => $v)
        {
            $this->data['datalist'][$k]['file_url']  = empty($v['file_url'])?URLPRE.'static/public/images/nophoto.jpg':site_url($v['file_url']);
            $this->data['datalist'][$k]['link']      = site_url('mobile/trips/profile/?id=' . $v['id']);
        }
        $this->data['keyword'] = $keyword;
        $this->data['page'] = $page;
        $city = SITECITYNAME ;
        $this->data['common'] = array(
            array(
                'head'   => '攻略' . ($keyword ? '-'.$keyword : $keyword),
                'title'   => '旅游攻略_游记_美食_'.$city.'旅游网',
                'keywords'   => $city.'旅游攻略,游记攻略',
                'description'   => $city.'旅游攻略，'.$city.'旅游网为您介绍'.SITECITYNAME.'旅游景点图片、游记、交通、美食、购物、住宿、娱乐、行程、指南等旅游攻略信息,了解'.$city.'旅游等自助游攻略信息来'.$city.'旅游网。'
            )
        );
        if(!empty($_GET['isajax']))
        {
            header('Content-type:application/json;charset=utf-8');
            echo json_encode($this->data['datalist']);
            exit;
        }
        else
        {
            $this->load->view(MOBILE_TEMPLATE_NAME.'/'.'trips_list', $this->data);
        }
    }

    public function profile()
    {
        $id = intval($_GET['id']);
        $this->data['info'] = $this->trips_model->get_trips_by_id($id);


        $message = strip_tags($this->data['info']['message'], '<img><a><p>');
        $patterns = array('/style=".*?"/i','/height=".*?"/i','/width=".*?"/i');
        $replacements = array(" "," "," ");
        $message = preg_replace($patterns,$replacements,$message);
        if($this->data['info']['file_url']){
            $cover = filter_img($this->data['info']['file_url']);
            $cover = "<img style='width:100%' src='$cover' />";
        }

        $city = SITECITYNAME ;
        $this->data['common'] = array(
            array(
                'head'   => $this->data['info']['subject'],
                'title'   => $this->data['info']['subject'].'_'.$city.'旅游网',
                'keywords'   => $city.'旅游攻略',
                'description'   => $city.'旅游网为您提供最新、最热门的'.$city.'旅游攻略等旅游攻略信息。'.$city.'旅游网'
            )
        );
        $this->load->view(MOBILE_TEMPLATE_NAME.'/'.'trips_profile', $this->data);
    }

}